package com.hy.heath.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.heath.mapper.HealthKnowledgeMapper;
import com.hy.heath.pojo.entity.HealthKnowledge;
import com.hy.heath.service.HealthKnowledgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class HealthKnowledgeServiceImpl extends ServiceImpl<HealthKnowledgeMapper,HealthKnowledge>implements HealthKnowledgeService {
    // 注入HealthKnowledgeMapper
    @Autowired
    private HealthKnowledgeMapper healthKnowledgeMapper;

    // 获取所有健康知识
    @Override
    public List<HealthKnowledge> getAllHealthKnowledge() {
        return healthKnowledgeMapper.getAllHealthKnowledge();
    }

    // 根据分类获取健康知识
    @Override
    public List<HealthKnowledge> listByCategory(String category) {
        // Use LambdaQueryWrapper to query the database
        return this.list(new LambdaQueryWrapper<HealthKnowledge>()
                // If the category is not empty, query the database based on the category
                .eq(StringUtils.hasText(category), HealthKnowledge::getCategory, category));
    }

    @Override
    public List<HealthKnowledge> listByAuthor(String author) {
        // Use LambdaQueryWrapper to query the database
        return this.list(new LambdaQueryWrapper<HealthKnowledge>()
                // If the author is not empty, query the database by author
                .eq(StringUtils.hasText(author), HealthKnowledge::getAuthor, author));
    }

    @Override
    public Page<HealthKnowledge> pageKnowledge(Page<HealthKnowledge> page, String category, String orderBy) {
        LambdaQueryWrapper<HealthKnowledge> queryWrapper = new LambdaQueryWrapper<>();

        // 分类条件
        queryWrapper.eq(StringUtils.hasText(category), HealthKnowledge::getCategory, category);

        // 动态排序逻辑
        if (StringUtils.hasText(orderBy)) {
            if ("view_count".equalsIgnoreCase(orderBy)) {
                queryWrapper.orderByDesc(HealthKnowledge::getViewCount);
            } else if ("like_count".equalsIgnoreCase(orderBy)) {
                queryWrapper.orderByDesc(HealthKnowledge::getLikeCount);
            }
        }

        return this.page(page, queryWrapper);
    }
    @Override
    @Transactional
    /**
     * 增加健康知识条目的浏览次数。
     *
     * @param knowledgeId 健康知识条目的ID
     */
    public void incrementViewCount(Integer knowledgeId) {
        this.update(new LambdaUpdateWrapper<HealthKnowledge>()
                .setSql("view_count = view_count + 1")
                .eq(HealthKnowledge::getKnowledgeId, knowledgeId));
    }
}
